<?php
class main extends spController
{
	function __construct()
	{
	        // 必须加入启动父类构造函数的操作
	        parent::__construct();
	         
	        // 开始全局操作
	        $this->arr_cat = Product_Cat();
			$this->a = $_GET['a'] ? $_GET['a'] : 'index';
	}

	function index_demo()
	{
		$m_youhui = spClass("youhui");
		$this->arr_cat_num = $m_youhui->spCache(3600)->findCat($this->arr_cat);
		$this->catid = $catid = $_GET['catid'] ? $_GET['catid'] : 0;
		$this->today = $_GET['today'];
		$this->valentine = $_GET['valentine'] ? $_GET['valentine'] : false;
		$this->mother == $_GET['mother'] ? $_GET['valentine'] : false;
		if($this->today == 'on') {
			$condition = "status=0 AND isdel=0 AND enddate>=curdate() AND startdate=curdate()";
		} elseif($this->valentine == 'on') {
			$condition = "status=0 AND isdel=0 AND enddate>=curdate() AND posid=3";
		} elseif($this->mother == 1) {
			$condition = " posid=4 AND status=0 AND isdel=0 AND enddate>=curdate()";
		} else {
			if($catid == 0) {
				$condition = " status=0 AND isdel=0 AND enddate>=curdate()";
			} else {
				$condition = " catid='$catid' AND status=0 AND isdel=0 AND enddate>=curdate()";
			}
		}

		$this->results = $m_youhui->spLinker()->spPager($this->spArgs('page', 1), 16)->findAll($condition, 'listorder DESC, enddate ASC');
		$this->pager = $m_youhui->spPager()->getPager();

		$this->today_total = spAccess('r',  'today_total');
		if(!$this->today_total) {
			$this->today_total = $m_youhui->findCount("status=0 AND isdel=0 AND enddate>=curdate() AND startdate=curdate()");
			spAccess('w',  'today_total', $this->today_total, 3600);
		}
		$this->total = spAccess('r',  'total');
		if(!$this->total) {
			$this->total = $m_youhui->findCount("status=0 AND isdel=0 AND enddate>=curdate()");
			spAccess('w',  'total', $this->total, 3600);
		}
		$this->arr_cid = spAccess('r',  'arr_cid');	// 类目缓存
		
		$this->display("index_demo.html");
	}

	function index()
	{		
		$m_youhui = spClass("youhui");
		$this->arr_cat_num = $m_youhui->spCache(3600)->findCat($this->arr_cat);
		$this->catid = $catid = $_GET['catid'] ? $_GET['catid'] : 0;
		$this->today = $_GET['today'];
		$this->valentine = $_GET['valentine'] ? $_GET['valentine'] : false;
		$this->mother = $_GET['mother'] ? $_GET['mother'] : false;
		if($this->today == 'on') {
			$condition = "status=0 AND isdel=0 AND enddate>=curdate() AND startdate=curdate()";
		} elseif($this->valentine == 'on') {
			$condition = "status=0 AND isdel=0 AND enddate>=curdate() AND posid=3";
		} elseif($this->mother == 1) {
			$condition = " posid=4 AND status=0 AND isdel=0 AND enddate>=curdate()";
		} else {
			if($catid == 0) {
				$condition = " status=0 AND isdel=0 AND enddate>=curdate()";
			} else {
				$condition = " catid='$catid' AND status=0 AND isdel=0 AND enddate>=curdate()";
			}
		}

		$this->results = $m_youhui->spLinker()->spPager($this->spArgs('page', 1), 16)->findAll($condition, 'listorder DESC, enddate ASC');
		$this->pager = $m_youhui->spPager()->getPager();

		$this->today_total = spAccess('r',  'today_total');
		if(!$this->today_total) {
			$this->today_total = $m_youhui->findCount("status=0 AND isdel=0 AND enddate>=curdate() AND startdate=curdate()");
			spAccess('w',  'today_total', $this->today_total, 3600);
		}
		$this->total = spAccess('r',  'total');
	
		if(!$this->total) {
			$this->total = $m_youhui->findCount("status=0 AND isdel=0 AND enddate>=curdate()");
			spAccess('w',  'total', $this->total, 3600);
		}
		$this->arr_cid = spAccess('r',  'arr_cid');	// 读取缓存
		
		$this->display("index.html");
	}

	function show()
	{
		$this->display("show_index.html");
	}

	function my()
	{
		$visitor_id = $_SESSION['visitor_id'] = '25106930';
		if($_SESSION['visitor_id'] == 0) $this->error('用户未登录，请登录以后再来访问', spUrl('main', 'index'));
		$this->my_avatar = json_decode(file_get_contents('http://yingyong.taobao.com/json/open/get_user_pic_url.htm?user_id=' . $_SESSION['visitor_id']))->user_pic_url;
		$m_collect_user = spClass('m_collect_user');
		$this->db_collect_result = $m_collect_user->spLinker()->findAll(array('visitor_id'=>$visitor_id));

		/*
		 * 当用户没有订阅时可以获取收藏夹里的信息
		 */
		if(empty($this->db_collect_result)) {
			$this->visitor_nick = $_SESSION['visitor_nick'];
			$this->collect_result = $m_collect_user->collect_result($this->visitor_nick);
		}
		//spAccess('w',  'collect_user_' . $_SESSION['visitor_id'], $this->collect_result, 604800);
		//var_dump($this->collect_result);exit;
		$this->display("my.html");
	}
}

?>